package com.example.myword;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import com.example.myword.dao.Word;
import com.example.myword.dao.WordDao;

import java.util.List;
import java.util.Random;


public class WordActivity extends AppCompatActivity {
    private TextView textViewWord;
    private TextView textViewMeaning;
    private TextView textViewWrongCount;
    private Button buttonKnown;
    private Button buttonUnknown;
    private AppDatabase db;
    private WordDao wordDao;
    private Word currentWord;
    private String userId = "current_user_id"; // Replace with actual user ID


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word);

        textViewWord = findViewById(R.id.textViewWord);
        textViewMeaning = findViewById(R.id.textViewMeaning);
        textViewWrongCount = findViewById(R.id.textViewWrongCount);
        buttonKnown = findViewById(R.id.buttonKnown);
        buttonUnknown = findViewById(R.id.buttonUnknown);

        db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "word-database").allowMainThreadQueries().build();
        wordDao = db.wordDao();
        userId = getIntent().getStringExtra("userId");

        String word = getIntent().getStringExtra("word");
        String meaning = getIntent().getStringExtra("meaning");

        if (word == null || meaning == null) {
            // If word or meaning is null, fetch a random word from the database
            currentWord = getRandomWordFromDatabase();
            if (currentWord != null) {
                word = currentWord.word;
                meaning = currentWord.meaning;
            }
        } else {
            // Get current word from database based on received word
            String finalWord = word;
            currentWord = wordDao.getAllWords(userId).stream().filter(w -> w.word.equals(finalWord)).findFirst().orElse(null);
        }

        textViewWord.setText(word);
        textViewMeaning.setText("--?--");
        // 单词语音播放
        Intent intent = new Intent(WordActivity.this, AudioService.class);
        intent.putExtra("query", word);
        startService(intent);

        if (currentWord != null) {
            textViewWrongCount.setText("Wrong Count: " + currentWord.getWrongCount());
        }

//        buttonKnown.setOnClickListener(v -> updateWordStatus(true));
//        buttonUnknown.setOnClickListener(v -> updateWordStatus(false));

        // 设置点击事件监听器
        buttonKnown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 加载动画
                Animation animation = AnimationUtils.loadAnimation(WordActivity.this, R.anim.button_click_animation);
                // 启动动画效果
                v.startAnimation(animation);
                updateWordStatus(true);
            }
        });

        buttonUnknown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 加载动画
                Animation animation = AnimationUtils.loadAnimation(WordActivity.this, R.anim.button_click_animation);
                // 启动动画效果
                v.startAnimation(animation);
                updateWordStatus(false);
            }
        });
    }

    private void updateWordStatus(boolean known) {
        if (currentWord != null) {
            currentWord.known = known;
            if (!known) {
                currentWord.wrongCount++;
                // 更新在数据库中currentWord
                wordDao.update(currentWord);
                textViewWrongCount.setText("错误次数: " + currentWord.getWrongCount());
                textViewMeaning.setText(currentWord.getMeaning());
                getRandomWordFromDatabase();
            } else {
                if(currentWord.wrongCount<=0){
                    Toast.makeText(this, "恭喜你学会了", Toast.LENGTH_SHORT).show();
                    textViewMeaning.setText(currentWord.getMeaning());
                    wordDao.deleteWordsForUser(userId, currentWord.getWord());
                }
               else {
                    currentWord.wrongCount--;
                    wordDao.update(currentWord);
                    textViewWrongCount.setText("错误次数: " + currentWord.getWrongCount());
                    textViewMeaning.setText(currentWord.getMeaning());
                    getRandomWordFromDatabase();
                }
            }

        }
    }

    private Word getRandomWordFromDatabase() {
        List<Word> allWords = wordDao.getAllWords(userId);
        if (allWords.isEmpty()) {
            return null;
        }
        Random random = new Random();
        return allWords.get(random.nextInt(allWords.size()));
    }

}
